package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.job.ums.entity.UmsCommunicate;
import org.jeecg.modules.job.ums.service.IUmsCommunicateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 *沟通记录模块管理
 */
@RestController
@RequestMapping("/api/comm")
@Api(tags="移动端沟通记录管理模块")
@Slf4j
public class CommController{

    @Autowired
    private IUmsCommunicateService communicateService;

    /**
     * 查询沟通列表
     */
    @RequestMapping(value = "/getCommList",method = RequestMethod.GET)
    @ApiOperation(value = "查询沟通列表", notes = "查询沟通列表")
    public Result<Object> getCommList(UmsCommunicate param,
                                      @RequestParam(name="page", defaultValue="1") Integer page,
                                      @RequestParam(name="limit", defaultValue="10") Integer limit
    ) {
        try{
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if(param.getType()==0){//用户端
                param.setMemberId(user.getId());
            }else{//企业端
                param.setComMemberId(user.getId());
            }
            IPage<UmsCommunicate> pageInfo= communicateService.findPageBySpec(new Page<>(page,limit),param);
            return Result.OK(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询沟通详情
     */
    @RequestMapping(value = "/getCommDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询沟通详情", notes = "查询沟通详情")
    public Result<Object> getCommDetail(UmsCommunicate param) {
        UmsCommunicate result=null;
        if (oConvertUtils.isNotEmpty(param.getId())){
            result=communicateService.getById(param.getId());
        }else{
            result=communicateService.getOne(new QueryWrapper<>(param));
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data",result);
        return Result.OK(map);
    }

    /**
     * 添加/更新沟通
     */
    @RequestMapping(value = "/addOrUpdateComm",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新沟通", notes = "添加/更新沟通")
    public Result<Object> addOrUpdateComm(@RequestBody UmsCommunicate param) {
        try {
            communicateService.addOrUpdate(param);
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除简历沟通
     */
    @RequestMapping(value = "/deleteComm",method = RequestMethod.POST)
    @ApiOperation(value = "删除沟通", notes = "删除沟通")
    public Result<Object> deleteComm(@RequestBody UmsCommunicate param) {
        try {
            communicateService.removeById(param.getId());
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}



